package com.qdxwx.web.controller.document;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKRequest;
import com.qdxwx.data.service.account.UserAccountService;
import com.qdxwx.data.service.category.CategoryService;
import com.qdxwx.data.service.document.DocumentService;
import com.qdxwx.data.service.favorite.FavoriteService;
import com.qdxwx.data.service.member.MemberRecordService;
import com.qdxwx.data.service.subject.SubjectService;
import com.qdxwx.data.service.user.UserBuyService;
import com.qdxwx.models.account.UserAccount;
import com.qdxwx.models.category.Category;
import com.qdxwx.models.document.Document;
import com.qdxwx.models.document.DocumentDetail;
import com.qdxwx.models.document.DocumentQuery;
import com.qdxwx.models.enums.DocumentType;
import com.qdxwx.models.enums.ModuleType;
import com.qdxwx.models.member.MemberRecord;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author MrGao
 */
@Controller
@RequestMapping("/document")
public class DocumentViewController extends BaseController {

    @Autowired
    private DocumentService documentService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private MemberRecordService memberRecordService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private FavoriteService favoriteService;
    @Autowired
    private UserBuyService userBuyService;
    @Autowired
    private UserAccountService userAccountService;

    @RequestMapping("/view/{id:[0-9]+}.html")
    public String documentView(@PathVariable("id") int documentId, HttpServletRequest request, ModelMap mmap) {
        Document document = documentService.get(documentId);
        if (document == null) {
            return toError404(request);
        }
        DocumentQuery query = new DocumentQuery();
        query.setStatus(1);
        query.setCategoryId(document.getCategoryId());
        query.setOrderBy(3);
        List<Document> documentList = documentService.getList(query, new PageRequest(1, 12));
        Category category = categoryService.get(document.getCategoryId());
        List<Category> categoryRoots = categoryService.getChildren(category.getParentId() > 0 ? category.getParentId() : category.getCategoryId());
        documentService.updateView(document.getId());
        long userId = LoginUser.getUserId(request);
        MemberRecord member = null;
        if (userId > 0) {
            boolean isBuy = userBuyService.checkIsBuy(userId, ModuleType.DOCUMENT.ordinal(),document.getId());
            boolean isFavorite = favoriteService.isExist(userId, document.getId(), ModuleType.DOCUMENT.ordinal());
            UserAccount account = userAccountService.getByUserId(userId);
            member = memberRecordService.getValidByUserId(userId, 1);
            mmap.put("isFavorite", isFavorite);
            mmap.put("isBuy", isBuy);
            mmap.put("userMoney", account.getBalance());
        } else {
            mmap.put("isFavorite", false);
            mmap.put("isBuy", false);
            mmap.put("userMoney", 0);
        }
        mmap.put("isVip", member != null);
        mmap.put("subjectName", subjectService.getName(document.getSubjectId()));
        mmap.put("typeName", DocumentType.getTitle(document.getTypeId()));
        mmap.put("userId", userId);
        mmap.put("document", document);
        mmap.put("documentList", documentList);
        mmap.put("categoryRoots", categoryRoots);
        if (TKRequest.isMobileBrowse(request)) {
            return getViewPathH5("document/document-view");
        }
        return getViewPath("document/document-view");
    }
}
